<?php


namespace app\index\controller;

use app\common\controller\Frontend;
use fast\Http;
use think\Db;

class Login  extends Frontend
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    //  元直播
    //  appid:SnXPsYz5KYgu
    //  secret:cSuXDTRWKwx4
    protected $Appsecret = [
        'appid' => 'SnXPsYz5KYgu',
        'secret' => 'cSuXDTRWKwx4'
    ];
    public function xyj()
    {
        $token  = $this->request->get('token');
        if(!empty($token))
        {

            $post_data = [
                'token' => $token,
            ];
            $timestamp = time();
            $sign = $this->xyj_sign($post_data, $timestamp, $this->Appsecret);
            // echo $sign;exit;
            $post_data = array_merge($post_data, [
                'appid' => $this->Appsecret['appid'],
                'source' => '59',
                'timestamp' => $timestamp,
                'func' => 'member.get_new_token',
            ]);

            $post_data['sign'] = $sign;
            $url = 'https://papi.zmxyj.com/open/att/api.json';

             //$url = "http://xyjgou-papi.pcbatt.com/open/att/api.json";

            $re = Http::sendRequest($url, $post_data, 'POST', [
                CURLOPT_TIMEOUT => 30,
                CURLOPT_CONNECTTIMEOUT => 30,
                // CURLOPT_PROXY => '47.109.54.233:8635',
                CURLOPT_PROXY => '172.16.0.224:3128',
                CURLOPT_VERBOSE => 1,
            ]);
            if($re['ret'] == 1)
            {
                /*
                Array
                (
                    [code] => 01
                    [msg] => 操作成功
                    [data] => Array
                        (
                            [token] => SDgzSmdUc1UwRzRNVGdrMkh1REVWYnBkWVlyOC9rUzMyV2NuMXNuSTRaUEppczgvNUsrajIrckt3V2x1cXoyaQ==
                            [info] => Array
                                (
                                    [telphone] => 13795543283
                                    [username] => yp
                                    [register_number] => 5823424
                                )

                        )

                )

                */
                $arr = json_decode($re['msg'],true);
                if($arr['code'] == '01')
                {
                    //TD($arr);
                    $new_token = $arr['data']['token'];
                    $mobile = $arr['data']['info']['telphone'];
                    $user = Db::name('user')->where([['username','=',$mobile]])->find();
                    if(!empty($user))
                    {
                        $this->auth->direct($user['id']);
                    }
                    else
                    {
                        $this->auth->register($mobile,  substr($mobile, -6), $mobile.'@163.com', $mobile,[
                            'nickname'=>'xyj'.$arr['data']['info']['register_number'],
                        ]);
                        Db::name('user_third')->insertGetId([
                            'user_id'=>$this->auth->id,
                            'platform'=>'xyj',
                            'openid'=>$arr['data']['info']['register_number'],
                            'nickname'=>$arr['data']['info']['username'],
                            'avatar'=>'',
                            'access_token'=>$new_token,
                            'refresh_token'=>$token,
                            'unionid'=>$arr['data']['info']['register_number'],
                            'expires_in'=>0,
                            'createtime'=>time(),
                        ]);
                    }
                    $this->assign('title','验证登录');
                    $this->assign('userInfo', json_encode($this->auth->getUserinfo()));
                    $this->assign('token', $this->auth->getToken());
                    $this->assign('title', '验证成功');
                    $this->assign('desc', '账号登录成功');

                    $this->assign('url', "/h5/");
                    return $this->view->fetch();
                }
                else
                {
                    $this->result('信易加返回:'.$arr['msg'],[],0,'json');

                }
            }
            else
            {
                $this->result('信易加通信接口错误',[],0,'json');
            }          //$this->success('请求成功','',$token);
        }
        else
        {
            $this->result('参数错误',[],0,'json');
        }
    }

    // 应用公共文件


    public function xyj_sign($data, $timestamp, $appSecret)
    {
        ksort($data);
        $sign = md5($appSecret['appid'] . $appSecret['secret'] . $timestamp . json_encode($data));
        //echo $appSecret['appid'] . $appSecret['secret'] . $timestamp . json_encode($data) . "\n";
        return $sign;
    }


}